local skel = fk.CreateSkill {
  name = "emo__shuangsheng",
  tags = {Skill.Switch},
}

Fk:loadTranslationTable{
  ["emo__shuangsheng"] = "双生",
  [":emo__shuangsheng"] = "转换技，当你造成或受到伤害后，阳：你可以交给一名其他角色至多X张牌，然后摸等量的牌。阴：你可以摸X张牌再弃置至体力上限，然后本回合你受到的伤害-1（X为你已损失体力值+1）。",

  [":emo__shuangsheng_yang"] = "转换技，当你造成或受到伤害后，<font color=\"#E0DB2F\">阳：你可以交给一名其他角色至多X张牌，然后摸等量的牌。</font>阴：你可以摸X张牌再弃置至体力上限，然后本回合你受到的伤害-1（X为你已损失体力值+1）。",
  [":emo__shuangsheng_yin"] = "转换技，当你造成或受到伤害后，阳：你可以交给一名其他角色至多X张牌，然后摸等量的牌。<font color=\"#E0DB2F\">阴：你可以摸X张牌再弃置至体力上限，然后本回合你受到的伤害-1（X为你已损失体力值+1）。</font>",

  ["#emo__shuangsheng-yang"] = "双生（阳）：你可选择至多 %arg 张牌，交给一名其他角色，然后你摸等量的牌",
  ["@emo__shuangsheng_damage-turn"] = "双生:减伤",
  ["#emo__shuangsheng-yin"] = "双生（阴）：可以摸%arg张牌再弃置至体力上限，本回合你受到的伤害减一",
}

local spec = {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and (player:getSwitchSkillState(skel.name) == fk.SwitchYin or not player:isNude())
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local x = player:getLostHp() + 1
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      local tos, cards = room:askToChooseCardsAndPlayers(player, {
        min_card_num = 1, max_card_num = x, max_num = 1, min_num = 1, skill_name = skel.name,
        pattern = ".", prompt = "#emo__shuangsheng-yang:::"..x,
        targets = room:getOtherPlayers(player, false),
      })
      if #cards > 0 and #tos > 0 then
        event:setCostData(self, {tos = tos, cards = cards })
        return true
      end
    else
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__shuangsheng-yin:::"..x}) then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost = event:getCostData(self) or Util.DummyTable
    if cost.cards then
      local cards = cost.cards
      room:obtainCard(cost.tos[1], cards, false, fk.ReasonGive, player, skel.name)
      if player:isAlive() then
        player:drawCards(#cards, skel.name)
      end
    else
      local x = player:getLostHp() + 1
      player:drawCards(x, skel.name)
      if player.dead then return end
      local y = player:getHandcardNum() - player.maxHp
      if y > 0 then
        room:askToDiscard(player, {min_num = y, max_num = y, include_equip = false, skill_name = skel.name, cancelable = false})
      end
      room:addPlayerMark(player, "@emo__shuangsheng_damage-turn")
    end
  end,
}

skel:addEffect(fk.Damage, {
  anim_type = "switch",
  can_trigger = spec.can_trigger,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "switch",
  can_trigger = spec.can_trigger,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skel:addEffect(fk.DamageInflicted, {
  is_delay_effect = true,
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@emo__shuangsheng_damage-turn") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data:changeDamage(-player:getMark("@emo__shuangsheng_damage-turn"))
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__shuangsheng_damage-turn", 0)
end)

return skel
